package ch.dreamweb.readpractice.client.lesetraining.widget;

import java.util.ArrayList;

import ch.dreamweb.readpractice.client.lesetraining.train.event.CorrectEvent;
import ch.dreamweb.readpractice.client.lesetraining.train.event.IncorrectEvent;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class ThreeButtonWidget extends Composite {

	private static final int TABSHIFT = 10;

	private static NineButtonWidgetUiBinder uiBinder = GWT
			.create(NineButtonWidgetUiBinder.class);

	interface NineButtonWidgetUiBinder extends
			UiBinder<Widget, ThreeButtonWidget> {
	}

	ArrayList<Button> buttons = new ArrayList<Button>();

	// @UiField
	// Grid grid;

	@UiField
	Button button1;

	@UiField
	Button button2;

	@UiField
	Button button3;

	private int random;

	private EventBus eventBus = null;

	public ThreeButtonWidget() {
		Log.debug("enter ThreeButtonWidget");

		initWidget(uiBinder.createAndBindUi(this));

		// grid.resize(1, 3);

		buttons.add(button1);
		buttons.add(button2);
		buttons.add(button3);

		for (int i = 0; i < buttons.size(); i++) {
			buttons.get(i).setTabIndex(TABSHIFT + i);
		}

		// setTexts(correct, others);
	}

	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	public void setTexts(String correct, String wrongOne, String wrongTwo) {
		random = (int) (Math.random() * 3);
		buttons.get(random).setText(correct);
		if (random == 0) {
			buttons.get(1).setText(wrongOne);
			buttons.get(2).setText(wrongTwo);
		} else if (random == 1) {
			buttons.get(0).setText(wrongOne);
			buttons.get(2).setText(wrongTwo);
		} else {
			buttons.get(0).setText(wrongOne);
			buttons.get(1).setText(wrongTwo);
		}

	}

	public void setEnabled(boolean enabled) {
		for (int i = 0; i < buttons.size(); i++) {
			buttons.get(i).setEnabled(enabled);
		}
	}

	@UiHandler("button1")
	void onButton1Click(ClickEvent e) {
		buttonClicked(0);
	}

	@UiHandler("button2")
	void onButton2Click(ClickEvent e) {
		buttonClicked(1);
	}

	@UiHandler("button3")
	void onButton3Click(ClickEvent e) {
		buttonClicked(2);
	}

	private void buttonClicked(int i) {
		if (i == random) {
			Log.info("Three correct.");
			if (eventBus != null) {
				eventBus.fireEvent(new CorrectEvent(1));
			}
		} else {
			String wrong = buttons.get(i).getText();
			Log.info("Three incorrect: " + wrong);
			if (eventBus != null) {
				eventBus.fireEvent(new IncorrectEvent(wrong, 1));
			}
		}
	}

	public void setFocus() {
		button1.setFocus(true);
	}

}
